#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=999999999,N=4000;
int n,q;
int match(int x,int y,int op){
	if(op==1)return (x&y);
	if(op==2)return (x|y);
	if(op==3)return (x^y);
	if(op==4)return (1ll*x+y)%mod;
}
int a[4005][4005];
long long sum1[4005][4005],sum2[4005][4005];
signed main(){
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>q;
	while(q--){
		int opt;
		cin>>opt;
		if(opt==1){
			int dir,i,op,w;
			cin>>dir>>i>>op>>w;
			if(dir==0){
				for(int j=n;j>=2;j--)sum1[i][j]-=sum1[i][j-1];
				for(int j=1;j<=n;j++){
					long long det=-a[i][j];
					a[i][j]=match(-det,w,op);
					det+=a[i][j];
					sum1[i][j]+=det;
					sum1[i][j]+=sum1[i][j-1];
				}
			}else{
				for(int j=n;j>=2;j--)sum2[j][i]-=sum2[j-1][i];
				for(int j=1;j<=n;j++){
					long long det=-a[j][i];
					a[j][i]=match(-det,w,op);
					det+=a[j][i];
					sum2[j][i]+=det;
					sum2[j][i]+=sum2[j-1][i];
				}
			}
			// if(dir==0)modify1(i,op,w);
			// else modify2(i,op,w);
		}else {
			int p,q;
			cin>>p>>q;
			long long ans=0;
			for(int i=1;i<=p;i++)ans+=sum1[i][q];
			for(int j=1;j<=q;j++)ans+=sum2[p][j];
			cout<<ans<<"\n";
			// cout<<query(p,q)<<"\n";
		}
	}

	return 0;
}